package ovm.gui.views.exam;

import com.vaadin.ui.Button;
import com.vaadin.ui.DateField;
import ovm.entity.StudyGroupEntity;
import ovm.entity.SubjectEntity;
import ovm.entity.TeacherEntity;
import ovm.gui.selectors.AbstractEntitySelector;
import ovm.gui.views.AbstractEntityListView;
import ovm.management.exam.ExamManager;

import java.util.Date;

import static ovm.application.MainApplication.*;

/**
 * Created by IntelliJ IDEA.
 * User: Jokser
 * Date: 14.06.12
 * Time: 23:49
 * To change this template use File | Settings | File Templates.
 */

public class ExamBlankListView extends AbstractEntityListView {

    private AbstractEntitySelector teacherSelector, subjectSelector, studyGroupSelector;
    private DateField examDateField;

    public ExamBlankListView(Class entityClass, Boolean editable) {
        super(entityClass, editable);
        table.removeContainerProperty("details");

        removeAllFilters();

        examDateField = new DateField("Дата экзамена");
        examDateField.setDateFormat("dd.MM.yyyy");
        examDateField.setValue(new Date());

        teacherSelector = new AbstractEntitySelector(TeacherEntity.class);
        subjectSelector = new AbstractEntitySelector(SubjectEntity.class);
        studyGroupSelector = new AbstractEntitySelector(StudyGroupEntity.class);

        teacherSelector.setCaption("Преподаватель");
        subjectSelector.setCaption("Предмет");
        studyGroupSelector.setCaption("Группа");

        advancedLayout.addComponent(examDateField);
        advancedLayout.addComponent(teacherSelector);
        advancedLayout.addComponent(subjectSelector);
        advancedLayout.addComponent(studyGroupSelector);

        editButton.setVisible(false);
        saveButton.setVisible(false);

        Button acceptButton = new Button("Занести результаты");
        acceptButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (ExamManager.acceptBlank((Date) examDateField.getValue(), (SubjectEntity) subjectSelector.getValue(), (StudyGroupEntity) studyGroupSelector.getValue()) == 0) {
                    navigator.showNotification("Бланк успешно добавлен!");
                } else {
                    navigator.showNotification("Произошла ошибка!");
                }
            }
        });

        Button clearButton = new Button("Очистить");
        clearButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                ExamManager.deleteBlank();
                examDateField.setValue(null);
                teacherSelector.setValue(null);
                subjectSelector.setValue(null);
                studyGroupSelector.setValue(null);
                entityContainer.refresh();
            }
        });

        actionsLayout.addComponent(clearButton);
        actionsLayout.addComponent(acceptButton);

    }
}
